﻿using System;
using System.Collections.Generic;
using ATSBLL;
using CrystalDecisions.CrystalReports.Engine;
using ATSModel; 
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Text;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class TestDataReport_BatchDataReport_simpleDateReport : System.Web.UI.Page
{
    private Report_PIMBatchData Pim;
    private Report_ElecBatchData Elec;
    private string str = "";
    private string st = "";

    #region 注释代码
    private DataSet ElecDataSet;
    private DataSet PIMDataSet;
    private DataTable ElecTable, PIMTable, newDataTable;
    private DataSet PIMandElecDataSet;
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        Elec = new Report_ElecBatchData();
        Pim = new Report_PIMBatchData();

        Load_DropList();
    }


    protected void Load_DropList()
    {
    }

    //通过“产品条码”搜索数据     2017/06/22 xl
    protected void ProSNSerach_Click(object sender, EventArgs e)
    {
        //str = "ProductSN";
        //BindGridView(str);
        string ProductSN = this.ProSN.Text;
        #region 注释代码
        ElecDataSet = Elec.ElecData_ByProSNS("ElecTable", ProductSN);
        PIMDataSet = Pim.PIMData_ByProSN("PIMTable", ProductSN);

        // 对获取的电性能数据表进行判断
        if (ElecDataSet.Tables.Count > 0)
            ElecTable = ElecDataSet.Tables[0];
        else
            ElecTable = new DataTable();

        // 对获取的交调数据表进行判断
        if (PIMDataSet.Tables.Count > 0)
            PIMTable = PIMDataSet.Tables[0];
        else
            PIMTable = new DataTable();

        // "newDataTable" 为电性能和交调详细数据拼接形成的
        newDataTable = JoinCls.myJoinMethod(PIMTable,ElecTable);

        GridView1.DataSource = newDataTable;
        GridView1.DataBind();
        #endregion
    }

    //通过“产品型号”搜索数据   2017/06/22 xl
 
    protected void ProTypeIDSerach_Click(object sender, EventArgs e)
    {
        //str = "ProTypeID";
        //BindGridView(str);

        string ProTypeID = this.ProTypeID.Text;
        #region 注释代码
        ElecDataSet = Elec.ElecData_ByProTypeIDS("ElecTable", ProTypeID);
        PIMDataSet = Pim.PIMData_ByProTypeID("PIMTable", ProTypeID);

        // 对获取的电性能数据表进行判断
        if (ElecDataSet.Tables.Count > 0)
            ElecTable = ElecDataSet.Tables[0];
        else
            ElecTable = new DataTable();

        // 对获取的交调数据表进行判断
        if (PIMDataSet.Tables.Count > 0)
            PIMTable = PIMDataSet.Tables[0];
        else
            PIMTable = new DataTable();

        // "newDataTable" 为电性能和交调详细数据拼接形成的
        newDataTable = JoinCls.myJoinMethod(PIMTable,ElecTable);

        GridView1.DataSource = newDataTable;
        GridView1.DataBind();
        #endregion
    }

    //通过“MO号”搜索数据  2017/06/22 xl
    protected void MOSerach_Click(object sender, EventArgs e)
    {
        //str = "MOID";
        //BindGridView(str);
        string MOID = this.ProMO1.Text;
        #region 注释代码
        ElecDataSet = Elec.ElecData_ByMOS("ElecTable", MOID);
        PIMDataSet = Pim.PIMData_ByMO("PIMTable", MOID);

        // 对获取的电性能数据表进行判断
        if (ElecDataSet.Tables.Count > 0)
            ElecTable = ElecDataSet.Tables[0];
        else
            ElecTable = new DataTable();

        // 对获取的交调数据表进行判断
        if (PIMDataSet.Tables.Count > 0)
            PIMTable = PIMDataSet.Tables[0];
        else
            PIMTable = new DataTable();

        // "newDataTable" 为电性能和交调详细数据拼接形成的
        newDataTable = JoinCls.myJoinMethod(PIMTable,ElecTable);

        GridView1.DataSource = newDataTable;
        GridView1.DataBind();
        #endregion
    }

    //通过复合条件搜索数据 
   //2017/06/22 xl
    protected void CompoundSearch_Click(object sender, EventArgs e)
    {
        //str = "MOAndDate";
        //BindGridView(str);
        string MO2 = this.ProMO2.Text;
        string dtS = this.startDate.Value;
        string dtE = this.endDate.Value;
        ElecDataSet = Elec.ELData_ByMOandDteS("ElecTable", MO2, dtS, dtE);
        PIMDataSet = Pim.PTData_ByMO("PIMTable", MO2, dtS, dtE);

        // 对获取的电性能数据表进行判断
        if (ElecDataSet.Tables.Count > 0)
            ElecTable = ElecDataSet.Tables[0];
        else
            ElecTable = new DataTable();

        // 对获取的交调数据表进行判断
        if (PIMDataSet.Tables.Count > 0)
            PIMTable = PIMDataSet.Tables[0];
        else
            PIMTable = new DataTable();

        // "newDataTable" 为电性能和交调详细数据拼接形成的
        newDataTable = JoinCls.myJoinMethod(PIMTable,ElecTable);

        GridView1.DataSource = newDataTable;
        GridView1.DataBind(); 
    } 

    /// <summary>
    /// 测试员与时间段检索
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void UserAndDTSearch_Click(object sender, EventArgs e)
    {
        string User = this.TBUser.Text;
        string dtS = this.startTime.Value;
        string dtE = this.endTime.Value;
        ElecDataSet = Elec.ELData_ByUserAndDteS("ElecTable", User, dtS, dtE);
        PIMDataSet = Pim.PTData_ByUserIDandDte("PIMTable", User, dtS, dtE);

        // 对获取的电性能数据表进行判断
        if (ElecDataSet.Tables.Count > 0)
            ElecTable = ElecDataSet.Tables[0];
        else
            ElecTable = new DataTable();

        // 对获取的交调数据表进行判断
        if (PIMDataSet.Tables.Count > 0)
            PIMTable = PIMDataSet.Tables[0];
        else
            PIMTable = new DataTable();

        // "newDataTable" 为电性能和交调详细数据拼接形成的
        newDataTable = JoinCls.myJoinMethod(PIMTable, ElecTable);

        GridView1.DataSource = newDataTable;
        GridView1.DataBind();

    }

    /// <summary>
    /// 时间段检索
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void DTSearch_Click(object sender, EventArgs e)
    {
        string dtS = this.sDate.Value;
        string dtE = this.eDate.Value;
        ElecDataSet = Elec.ELData_ByDtes("ElecTable",  dtS, dtE);
        PIMDataSet = Pim.PTData_ByDate("PIMTable", dtS, dtE);

        // 对获取的电性能数据表进行判断
        if (ElecDataSet.Tables.Count > 0)
            ElecTable = ElecDataSet.Tables[0];
        else
            ElecTable = new DataTable();

        // 对获取的交调数据表进行判断
        if (PIMDataSet.Tables.Count > 0)
            PIMTable = PIMDataSet.Tables[0];
        else
            PIMTable = new DataTable();

        // "newDataTable" 为电性能和交调详细数据拼接形成的
        newDataTable = JoinCls.myJoinMethod(PIMTable, ElecTable);

        GridView1.DataSource = newDataTable;
        GridView1.DataBind();
    }

    //分页 2017/05/12 xl
    //protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    //{
    //    //this.GridView1.PageIndex = e.NewPageIndex;
    //    //string st = this.Hiddenstr.Value;
    //    //BindGridView(st);

    //}
    // 绑定数据 2017/05/12 xl
    //public void BindGridView(string info)
    //{ 
    //    switch (info)
    //    {
    //        case "MOID":
    //            if (!string.IsNullOrEmpty(this.ProMO1.Text))
    //            {
    //                string MOID = this.ProMO1.Text;
    //                GridView1.DataSource = Pim.PIMData_ByMO(MOID);
    //                GridView1.DataBind();
    //                this.Hiddenstr.Value = "MOID"; 
    //            } 
    //            break;
    //        case "ProTypeID":
    //            if (!string.IsNullOrEmpty(this.ProTypeID.Text))
    //            {
    //                string ProTypeID = this.ProTypeID.Text;
    //                GridView1.DataSource = Pim.PIMData_ByProTypeID(ProTypeID);
    //                GridView1.DataBind();
    //                this.Hiddenstr.Value = "ProTypeID";
    //            }
    //            break;
    //        case "ProductSN":
    //            if (!string.IsNullOrEmpty(this.ProSN.Text))
    //            {
    //                string ProductSN = this.ProSN.Text;
    //                GridView1.DataSource = Pim.PIMData_ByProSN(ProductSN);
    //                GridView1.DataBind();
    //                this.Hiddenstr.Value = "ProductSN";
    //            }
    //            break;
    //        case "MOAndDate":
    //            if (!string.IsNullOrEmpty(this.ProMO2.Text) && !string.IsNullOrEmpty(this.startDate.Value) && !string.IsNullOrEmpty(this.endDate.Value))
    //            {
    //                string MO2 = this.ProMO2.Text;
    //                string dtS = this.startDate.Value;
    //                string dtE = this.endDate.Value;
    //                GridView1.DataSource = Pim.PTData_ByMO(MO2, dtS, dtE);
    //                GridView1.DataBind();
    //                this.Hiddenstr.Value = "MOAndDate";
    //            }
    //            break;
    //        default: 
    //            break;
    //    }


    //}



    // 最终报表生成按钮 
    // 2017/05/12 xl
    protected void Button1_Click(object sender, EventArgs e)
    {
        //ExportDataGrid("application/ms-excel", "天线批量数据.xls");


        //导出全部数据，取消分页
        //GridView1.AllowPaging = false;
        //GridView1.ShowFooter = false;

        //BindGridView(this.Hiddenstr.Value);

        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "UTF-8";
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode("天线批量数据") + ".xls");
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");//设置输出流为简体中文

        Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
        this.EnableViewState = false;
        //System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
        //System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
        //System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

        //ClearControls(GridView1);
        //this.GridView1.RenderControl(oHtmlTextWriter);
        //Response.Write(oStringWriter.ToString());
        //Response.End();



        //定义一个输入流　　   
        StringWriter tw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(tw);

        //目标数据绑定到输入流输出　  
        GridView1.RenderControl(hw);

        Response.Write(tw.ToString());
        Response.End();
        //还原分页显示
        //GridView1.AllowPaging = true;
        //GridView1.ShowFooter = true;
        //BindGridView(this.Hiddenstr.Value);

    }




    /// <summary>
    /// 清除控件中的所有控件，以便导出Excel
    /// </summary>
    /// <param name="control"></param>
    /// 2017/06/22 xl
    private void ClearControls(Control control)
    {
        for (int i = control.Controls.Count - 1; i >= 0; i--)
        {
            ClearControls(control.Controls[i]);
        }

        if (!(control is TableCell))
        {
            if (control.GetType().GetProperty("SelectedItem") != null)
            {
                LiteralControl literal = new LiteralControl();
                control.Parent.Controls.Add(literal);
                try
                {
                    literal.Text = (string)control.GetType().GetProperty("SelectedItem").GetValue(control, null);
                }
                catch
                {
                }
                control.Parent.Controls.Remove(control);
            }
            else if (control.GetType().GetProperty("Text") != null)
            {
                LiteralControl literal = new LiteralControl();
                control.Parent.Controls.Add(literal);
                literal.Text = (string)control.GetType().GetProperty("Text").GetValue(control, null);
                control.Parent.Controls.Remove(control);
            }
        }
        return;
    }

    // 解决“类型"GridView"的控件必须放在具有runat=server的窗体标记内”问题
    // 必须位于 <form runat=server> 标记中的控件可以在呈现之前调用此方法，以便在控件被置于标记外时显示错误信息
    public override void VerifyRenderingInServerForm(Control control)
    {

    }
   
}
